package com.ccnu.JobHub.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccnu.JobHub.model.common.Result;
import com.ccnu.JobHub.model.database.*;
import com.ccnu.JobHub.model.request.AddPostTagRequest;
import com.ccnu.JobHub.model.request.AddStudentTagRequest;
import com.ccnu.JobHub.model.request.TagForPostRequest;
import com.ccnu.JobHub.model.request.TagForStudentRequest;
import com.ccnu.JobHub.service.impl.PostTagService;
import com.ccnu.JobHub.service.impl.StudentTagService;
import com.ccnu.JobHub.service.impl.TagForPostService;
import com.ccnu.JobHub.service.impl.TagForStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@CrossOrigin("*")
@RequestMapping("tag")
public class TagController {
    private final StudentTagService studentTagService;
    private final PostTagService postTagService;
    private final TagForPostService tagForPostService;
    private final TagForStudentService tagForStudentService;

    public TagController(StudentTagService studentTagService, PostTagService postTagService, TagForPostService tagForPostService, TagForStudentService tagForStudentService) {
        this.studentTagService = studentTagService;
        this.postTagService = postTagService;
        this.tagForPostService = tagForPostService;
        this.tagForStudentService = tagForStudentService;
    }

    /**
     * 添加帖子标签，可以有重复的，会提前检索一遍以防重复
     * @param addPostTagRequest 一个列表包含帖子的标签名
     * @return 一个Result结果
     */
    @PostMapping("addPostTag")
    public Result addPostTag(@RequestBody AddPostTagRequest addPostTagRequest){
        log.info("--------------------添加帖子标签请求--------------------");

        for (String tag: addPostTagRequest.getTags()){
            QueryWrapper<TagForPost> tagForPostQueryWrapper = new QueryWrapper<TagForPost>()
                    .eq("tag_name",tag);
            TagForPost tagForPost = tagForPostService.getOne(tagForPostQueryWrapper);
            if (tagForPost==null) {
                TagForPost tagForPost1 = new TagForPost();
                tagForPost1.setTagName(tag);
                tagForPostService.save(tagForPost1);
            }
        }
        return Result.success("添加帖子标签成功");
    }

    /**
     * 显示所有的帖子标签
     * @return 返回一个列表，每个列表里面都是以map形式存在
     */
    @GetMapping("showPostTag")
    public Result showPostTag(){
        log.info("--------------------显示帖子标签列表请求--------------------");
        List<TagForPost> tag= tagForPostService.list();
        if (tag.isEmpty()){
            return Result.error("无帖子标签");
        }
        else return Result.success("显示帖子标签列表成功" ,tag);
    }

    /**
     * 删除帖子标签
     * @param deletePostRequest 内含一个标签id
     * @return Result结果
     */
    @DeleteMapping("deletePostTag")
    public Result deletePostTag(@RequestBody Map<String,String > deletePostRequest){
        log.info("--------------------删除帖子标签请求--------------------");
        boolean a = tagForPostService.removeById(deletePostRequest.get("tagId"));
        return a ? Result.success("删除帖子标签成功"): Result.error("删除帖子标签失败");
    }

    /**
     * 关联帖子与标签
     * @param tagForPostRequest 关联帖子与标签请求
     * @return Result结果
     */
    @PostMapping("addTagForPost")
    public Result addTagForPost(@RequestBody TagForPostRequest tagForPostRequest){
        log.info("--------------------关联帖子与标签请求--------------------");
        PostTag postTag = new PostTag();
        QueryWrapper<PostTag> postTagQueryWrapper = new QueryWrapper<PostTag>()
                .eq("post_id", tagForPostRequest.getPostId())
                .eq("tag_id", tagForPostRequest.getTagId());
        PostTag postTag1 = postTagService.getOne(postTagQueryWrapper);
        if (postTag1 == null){
            postTag.setPostId(tagForPostRequest.getPostId());
            postTag.setTagId(tagForPostRequest.getTagId());
            postTagService.save(postTag);
            return Result.success("关联帖子与标签成功");
        }
        else return Result.error("当前帖子已有该标签");
    }

    /**
     * 删除帖子与标签的关联
     * @param tagForPostRequest 帖子与标签请求
     * @return Result结果
     */
    @DeleteMapping("deleteTagForPost")
    public Result deleteTagForPost(@RequestBody TagForPostRequest tagForPostRequest){
        log.info("--------------------删除帖子与标签的关联请求--------------------");

        QueryWrapper<PostTag> postTagQueryWrapper = new QueryWrapper<PostTag>()
                .eq("post_id",tagForPostRequest.getPostId())
                .eq("tag_id",tagForPostRequest.getTagId());
        PostTag postTag = postTagService.getOne(postTagQueryWrapper);
        if (postTag == null){
            return Result.error("不存在该关联");
        }
        else {
            postTagService.remove(postTagQueryWrapper);
            return Result.success("删除帖子与标签的关联成功");
        }
    }

    /**
     * 显示帖子关联标签
     * @param postId 帖子id
     * @return Result结果
     */
    @GetMapping("showTagForPost")
    public Result showTagForPost(@RequestParam("postId") Integer postId){
        log.info("--------------------显示帖子关联标签请求--------------------");
        QueryWrapper<PostTag> postTagQueryWrapper = new QueryWrapper<PostTag>()
                .eq("post_id",postId);
        List<PostTag> postTags = postTagService.list(postTagQueryWrapper);

        List<Map<String,Object>> result = new ArrayList<>();
        if (postTags.isEmpty()){
            return Result.error("当前帖子无关联标签");
        }
        else {
            for (PostTag postTag : postTags){
                TagForPost tagForPost;
                tagForPost = tagForPostService.getById(postTag.getTagId());
                Map<String,Object> part = new HashMap<>();
                part.put("tagId",tagForPost.getTagId());
                part.put("tagName",tagForPost.getTagName());
                result.add(part);
            }
            return Result.success("显示帖子关联标签成功",result);
        }
    }

    /**
     * 搜索帖子标签请求
     * @param search 搜索内容
     * @param page 当前页码
     * @param pageSize 页面大小
     * @return 返回一个Result结果
     */
    @GetMapping("searchPostTag")
    public Result searchPostTag(@RequestParam("search") String search,@RequestParam("page") Integer page,@RequestParam("pageSize") Integer pageSize){
        log.info("--------------------搜索帖子标签请求--------------------");
        QueryWrapper<TagForPost> tagForPostQueryWrapper = new QueryWrapper<TagForPost>()
                .like("tag_name",search);
        Page<TagForPost> tagForPostPage =Page.of(page,pageSize);
        Page<TagForPost> p = tagForPostService.page(tagForPostPage,tagForPostQueryWrapper);
        List<TagForPost> tagForPosts = p.getRecords();
        if (tagForPosts.isEmpty()){
            return Result.error("未搜索到标签");
        }
        else{
            return Result.success("成功搜索帖子标签",tagForPosts);
        }
    }

    /**
     * 添加用户标签，可以有重复的，会提前检索一遍以防重复
     * @param addStudentTagRequest 一个列表包含用户的标签名
     * @return 一个Result结果
     */
    @PostMapping("addStudentTag")
    public Result addStudentTag(@RequestBody AddStudentTagRequest addStudentTagRequest){
        log.info("--------------------添加用户标签请求--------------------");

        for (String tag: addStudentTagRequest.getTags()){
            QueryWrapper<TagForStudent> tagForStudentQueryWrapper = new QueryWrapper<TagForStudent>()
                    .eq("tag_name",tag);
            TagForStudent tagForStudent = tagForStudentService.getOne(tagForStudentQueryWrapper);
            if (tagForStudent==null) {
                TagForStudent tagForStudent1 = new TagForStudent();
                tagForStudent1.setTagName(tag);
                tagForStudentService.save(tagForStudent1);
            }
        }
        return Result.success("添加用户标签成功");
    }

    /**
     * 显示所有的用户标签
     * @return 返回一个列表，每个列表里面都是以map形式存在
     */
    @GetMapping("showStudentTag")
    public Result showStudentTag(){
        log.info("--------------------显示用户标签列表请求--------------------");
        List<TagForStudent> tag= tagForStudentService.list();
        if (tag.isEmpty()){
            return Result.error("无用户标签");
        }
        else return Result.success("显示用户标签列表成功" ,tag);
    }

    /**
     * 删除用户标签
     * @param deleteStudentRequest 内含一个标签id
     * @return Result结果
     */
    @DeleteMapping("deleteStudentTag")
    public Result deleteStudentTag(@RequestBody Map<String,String > deleteStudentRequest){
        log.info("--------------------删除用户标签请求--------------------");
        boolean a = tagForStudentService.removeById(deleteStudentRequest.get("tagId"));
        return a ? Result.success("删除用户标签成功"): Result.error("删除用户标签失败");
    }

    /**
     * 关联用户与标签
     * @param tagForStudentRequest 关联用户与标签请求
     * @return Result结果
     */
    @PostMapping("addTagForStudent")
    public Result addTagForStudent(@RequestBody TagForStudentRequest tagForStudentRequest){
        log.info("--------------------关联用户与标签请求--------------------");
        StudentTag studentTag = new StudentTag();
        QueryWrapper<StudentTag> studentTagQueryWrapper = new QueryWrapper<StudentTag>()
                .eq("student_id", tagForStudentRequest.getStudentId())
                .eq("tag_id", tagForStudentRequest.getTagId());
        StudentTag studentTag1 = studentTagService.getOne(studentTagQueryWrapper);
        if (studentTag1 == null){
            studentTag.setStudentId(tagForStudentRequest.getStudentId());
            studentTag.setTagId(tagForStudentRequest.getTagId());
            try {
                studentTagService.save(studentTag);
                return Result.success("关联用户与标签成功");
            }catch (Exception e){
                return Result.error("数据库出错");
            }

        }
        else return Result.error("当前用户已有该标签");
    }

    /**
     * 删除用户与标签的关联
     * @param tagForStudentRequest 用户与标签请求
     * @return Result结果
     */
    @DeleteMapping("deleteTagForStudent")
    public Result deleteTagForStudent(@RequestBody TagForStudentRequest tagForStudentRequest){
        log.info("--------------------删除用户与标签的关联请求--------------------");
        QueryWrapper<StudentTag> studentTagQueryWrapper = new QueryWrapper<StudentTag>()
                .eq("student_id",tagForStudentRequest.getStudentId())
                .eq("tag_id",tagForStudentRequest.getTagId());
        StudentTag studentTag = studentTagService.getOne(studentTagQueryWrapper);
        if (studentTag == null){
            return Result.error("不存在该关联");
        }
        else {
            studentTagService.remove(studentTagQueryWrapper);
            return Result.success("删除用户与标签的关联成功");
        }
    }

    /**
     * 显示用户关联标签
     * @param studentId 用户id
     * @return Result结果
     */
    @GetMapping("showTagForStudent")
    public Result showTagForStudent(@RequestParam("studentId") Integer studentId){
        log.info("--------------------显示用户关联标签请求--------------------");
        QueryWrapper<StudentTag> studentTagQueryWrapper = new QueryWrapper<StudentTag>()
                .eq("student_id",studentId);
        List<StudentTag> studentTags = studentTagService.list(studentTagQueryWrapper);

        List<Map<String,Object>> result = new ArrayList<>();
        if (studentTags.isEmpty()){
            return Result.error("当前用户无关联标签");
        }
        else {
            for (StudentTag studentTag : studentTags){
                TagForStudent tagForStudent;
                tagForStudent = tagForStudentService.getById(studentTag.getTagId());
                Map<String,Object> part = new HashMap<>();
                part.put("tagId",tagForStudent.getTagId());
                part.put("tagName",tagForStudent.getTagName());
                result.add(part);
            }
            return Result.success("显示用户关联标签成功",result);
        }
    }

    /**
     * 搜索用户标签请求
     * @param search 搜索内容
     * @param page 当前页码
     * @param pageSize 页面大小
     * @return 返回一个Result结果
     */
    @GetMapping("searchStudentTag")
    public Result searchStudentTag(@RequestParam("search") String search,@RequestParam("page") Integer page,@RequestParam("pageSize") Integer pageSize){
        log.info("--------------------搜索用户标签请求--------------------");
        QueryWrapper<TagForStudent> tagForStudentQueryWrapper = new QueryWrapper<TagForStudent>()
                .like("tag_name",search);
        Page<TagForStudent> tagForStudentPage =Page.of(page,pageSize);
        Page<TagForStudent> p = tagForStudentService.page(tagForStudentPage,tagForStudentQueryWrapper);
        List<TagForStudent> tagForStudents = p.getRecords();
        if (tagForStudents.isEmpty()){
            return Result.error("未搜索到标签");
        }
        else{
            return Result.success("成功搜索用户标签",tagForStudents);
        }
    }
}
